import { getUserAddition } from '@/api/account'
import { getPersonalPreference } from '@/api/generalSetting'
import { defineStore } from 'pinia'

export type State = {
  valleyElecStartTime: string
  valleyElecEndTime: string
  messageStartTime: string
  messageEndTime: string
  isSetValleyTime: boolean
  isNeedDeleteWxuserPairWiFis: boolean
  sharetoken: string
  ownerName: string
}
export type Getters = {}
export type Actions = {
  loadValleyData(): Promise<void>
  undateElecTime(start: string, end: string, isUpdate: boolean): void
  updateMessageTime(start: string, end: string): void
  undateIsNeedDeletePairWiFis(value: boolean): void
  undateSharetoken(value: string): void
  undateOwnerName(value: string): void
}

export const useAccountStore = defineStore<'account', State, Getters, Actions>('account', {
  state: () => ({
    valleyElecStartTime: '21:00',
    valleyElecEndTime: '07:00',
    messageStartTime: '21:00',
    messageEndTime: '07:00',
    isSetValleyTime: false,
    isNeedDeleteWxuserPairWiFis: false,
    sharetoken: '',
    ownerName: '',
  }),
  getters: {},
  actions: {
    async loadValleyData() {
      const res = await getPersonalPreference()
      if (res.data.valleyTime) {
        this.isSetValleyTime = res.data.valleyTime.enabled === 1
        this.valleyElecStartTime = res.data.valleyTime.startTime
        this.valleyElecEndTime = res.data.valleyTime.endTime
      }
    },
    undateElecTime(start, end, isUpdate) {
      this.valleyElecStartTime = start
      this.valleyElecEndTime = end
      if (isUpdate) this.isSetValleyTime = true
    },
    updateMessageTime(start, end) {
      this.messageStartTime = start
      this.messageEndTime = end
    },
    undateIsNeedDeletePairWiFis(value) {
      this.isNeedDeleteWxuserPairWiFis = value
    },
    undateSharetoken(value) {
      this.sharetoken = value
    },
    undateOwnerName(value) {
      this.ownerName = value
    },
  },
})
